/*
 * Package il.ac.biu.cs.grossmm.api.data
 * File QueryProcessor.java
 * 
 * This is a part of presence service framework API. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA  02110-1301, USA.
 */

package il.ac.biu.cs.grossmm.api.flow;

import il.ac.biu.cs.grossmm.api.OperationFailedException;
import il.ac.biu.cs.grossmm.api.data.Node;
import il.ac.biu.cs.grossmm.api.data.Nodeset;
import il.ac.biu.cs.grossmm.api.data.Property;

import java.util.HashSet;
import java.util.Set;

/**
 * Used to run simple queries on data ndoes
 */
public class QueryProcessor<N> {
	protected final NodeInspector inspector;

	protected final Node<N> node;

	public QueryProcessor(NodeInspector inspector, Node<N> node)
	{
		this.inspector = inspector;
		this.node = node;
	}

	/**
	 * Finds a subnode of a given node in a given nodeset where given property
	 * value equals to the given value
	 * 
	 * @param <S>
	 *            node type class
	 * @param <V>
	 *            nodeset type class
	 * @param nodeSet
	 *            the nodeset
	 * @param prop
	 *            the property
	 * @param value
	 *            the value to match
	 * @return some subnode of a given node in a given nodeset where given
	 *         property value equals to the given value. If there are serveral
	 *         such nodes, only one (arbitrary) is returned
	 * @throws OperationFailedException when operation failed
	 * 
	 * @see findAll
	 */
	public <S, V> Node<S> find(Nodeset<N, S> nodeSet, Property<S, V> prop,
		V value) throws OperationFailedException
	{
		Set<Node<S>> nodes = inspector.getSubnodes(node, nodeSet);

		for (Node<S> n : nodes) {
			V v = inspector.getValue(n, prop);
			if (v == null) {
				if (value == null)
					return n;
			} else {
				if (v.equals(value))
					return n;
			}
		}

		return null;
	}

	/**
	 * Queries whether there a given node contains a subnode in a given nodeset
	 * where given property value equals to the given value
	 * 
	 * @param <S>
	 *            node type class
	 * @param <V>
	 *            nodeset type class
	 * @param nodeSet
	 *            the nodeset
	 * @param prop
	 *            the property
	 * @param value
	 *            the value to match
	 * @return true if there a given node contains a subnode in a given nodeset
	 * where given property value equals to the given value, false if not
	 * @throws OperationFailedException when operation failed
	 */
	public <S, V> boolean contains(Nodeset<N, S> nodeSet, Property<S, V> prop,
		V value) throws OperationFailedException
	{
		return find(nodeSet, prop, value) != null;
	}

	/**
	 * Finds all subnodes of a given node in a given nodeset where given property
	 * value equals to the given value
	 * 
	 * @param <S>
	 *            node type class
	 * @param <V>
	 *            nodeset type class
	 * @param nodeSet
	 *            the nodeset
	 * @param prop
	 *            the property
	 * @param value
	 *            the value to match
	 * @return all subnodes of a given node in a given nodeset where given
	 *         property value equals to the given value.
	 * @throws OperationFailedException when operation failed
	 * 
	 * @see find
	 */
	public <S, V> Set<Node<S>> findAll(Nodeset<N, S> nodeSet,
		Property<S, V> prop, V value) throws OperationFailedException
	{
		Set<Node<S>> nodes = inspector.getSubnodes(node, nodeSet);

		HashSet<Node<S>> set = null;

		for (Node<S> n : nodes) {
			V v = inspector.getValue(n, prop);
			if (v == null ? value == null : v.equals(value)) {
				if (value == null) {
					if (set == null)
						set = new HashSet<Node<S>>();
					set.add(n);
				}
			}
		}

		return null;
	}
}
